/*
 * LogEx a Java library for extracting log file data
 * Copyright 2012 LogEx Team.
 *
 * This program is free software. You can distribute or modify it under the 
 * terms of the GNU Lesser General Public License version 3.0 as published by  
 * the Free Software Foundation.
 *
 * This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
 * INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, contact the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package org.logex.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import org.ikayzo.sdl.Tag;
import org.logex.obj.LinePattern;

/**
 * Converts a given <code>Tag</code> object into an easily accessible and
 * iterable version of an object.
 * 
 * @author Lohitha Chiranjeewa
 */
public class RootObject {

	private Tag mainChild;
	private int numOfInnerChildren;
	private int numOfOuterChildren;
	private List<Object> siblingPatterns;
	private ArrayList<SortedMap<String, Object>> mainList;
	private SortedMap<String, Object> attributeMap;
	private SortedMap<String, Object> secondaryMap;
	private HashMap<String, LinePattern> mainMap;

	/**
	 * Constructor for <code>RootObject</code> class.
	 * 
	 * @param root
	 *            Incoming <code>Tag</code> object
	 */
	public RootObject(Tag root) {
		mainChild = root.getChild(0);
		mainMap = new LinkedHashMap<String, LinePattern>();
		numOfOuterChildren = mainChild.getChildCount();
		for (int j = 0; j < numOfOuterChildren; j++) {
			siblingPatterns = new ArrayList<Object>();
			mainList = new ArrayList<SortedMap<String, Object>>();
			Tag child = mainChild.getChild(j);
			siblingPatterns = child.getValues();
			String outerChildName = child.getName();
			numOfInnerChildren = child.getChildCount();
			for (int i = 0; i < numOfInnerChildren; i++) {
				Tag innermostChild = child.getChild(i);
				String childName = innermostChild.getName();
				attributeMap = innermostChild.getAttributes();
				secondaryMap = new TreeMap<String, Object>();
				secondaryMap.put("title", childName);
				secondaryMap.putAll(attributeMap);
				mainList.add(secondaryMap);
			}
			mainMap.put(outerChildName, new LinePattern(siblingPatterns,
					mainList));
		}
	}

	/**
	 * @return mainMap The map with containing all log file line patterns
	 */
	public HashMap<String, LinePattern> getMainMap() {
		return mainMap;
	}

}
